/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      IRConfig.cpp
 *
 * $FTS$
 */

#include <IRConfig.hpp>

namespace FTSS // FullTextSearchSystem
{

/**
  @brief Constructor
*/
IRConfig::IRConfig(const UINT_32 & iKeyCount): oConfigHash(iKeyCount) { ;; }

/**
  @brief Add pair of (key, value)
  @param sKey - key
  @param sValue - value
  @return 0 if success, -1 if duplicate key found
*/
INT_32 IRConfig::AddKey(const STLW::string & sKey, const STLW::string & sValue)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);
	if (ithData != oConfigHash.end()) { return -1; }
	oConfigHash.insert(ConfigPair(iHash, sValue), iHash);
	return 0;
}

/**
  @brief Replace pair of (key, value)
  @param sKey - key
  @param sValue - value
  @return 0 if success, -1 if duplicate key found
*/
INT_32 IRConfig::ReplaceKey(const STLW::string & sKey, const STLW::string & sValue)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);
	if (ithData != oConfigHash.end()) { ithData -> second.assign(sValue); return 0; }
	oConfigHash.insert(ConfigPair(iHash, sValue), iHash);
	return 0;
}

/**
  @brief Get 32-bit integer point value
  @param sKey - key
  @param iDefault - default value
  @return key value or default value if key doesn't exist
*/
INT_32 IRConfig::GetInt32(const STLW::string & sKey, const INT_32 & iDefault)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);

	if (ithData == oConfigHash.end()) { return iDefault; }
	return atoi(ithData -> second.c_str());
}

/**
  @brief Get 64-bit integer point value
  @param sKey - key
  @param iDefault - default value
  @return key value or default value if key doesn't exist
*/
INT_64 IRConfig::GetInt64(const STLW::string & sKey, const INT_64 & iDefault)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);

	if (ithData == oConfigHash.end()) { return iDefault; }
	return atol(ithData -> second.c_str());
}

/**
  @brief Get floating point value
  @param sKey - key
  @param dDefault - default value
  @return key value or default value if key doesn't exist
*/
W_FLOAT IRConfig::GetFloat(const STLW::string & sKey, const W_FLOAT & dDefault)
{
	W_FLOAT dRetVal = dDefault;
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);

	if (ithData != oConfigHash.end()) { dRetVal = atof(ithData -> second.c_str()); }

return dRetVal;
}

/**
  @brief Get STLW::string value
  @param sKey - key
  @param sDefault - default value
  @return key value or default value if key doesn't exist
*/
const STLW::string & IRConfig::GetString(const STLW::string & sKey, const STLW::string & sDefault)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);

	if (ithData == oConfigHash.end()) { return sDefault; }
	return ithData -> second;
}

/**
  @brief Get Flag
  @param sKey - key
  @param iDefault - default value
  @return key value or default value if key doesn't exist
*/
CHAR_8 IRConfig::GetFlag(const STLW::string & sKey, const CHAR_8 & iDefault)
{
	TokenHash_t iHash = HashFunc(sKey.c_str(), sKey.length());
	ConfigHash::iterator ithData = oConfigHash.find(iHash, iHash);

	if (ithData == oConfigHash.end() || ithData -> second.size() == 0) { return iDefault; }

	// Y[es] / y[es] / T[rue] / t[rue] / On
	if (ithData -> second[0] == 'Y' || ithData -> second[0] == 'y' ||
	    ithData -> second[0] == 'T' || ithData -> second[0] == 't' ||
	    (ithData -> second.length() == 2 &&
	    ((ithData -> second[0] == 'O' || ithData -> second[0] == 'o') &&
	     (ithData -> second[1] == 'N' || ithData -> second[1] == 'n')))) { return 1; }
	return 0;
}

} // namespace FTSS
// End.
